#include <pool.h>
#include <string.h>

#include <kern/debug.h>

void pool_init(struct pool_t* pool, int link_offset)
{
	memset(pool, 0, sizeof *pool);
	pool->link_offset = link_offset;
}

void pool_add(struct pool_t* pool, void* obj)
{
	struct link_t* link;

	link = (struct link_t*)((uintptr_t)obj + pool->link_offset);
	
	link->next = pool->first;
	pool->first = link;
}

void* pool_pop(struct pool_t* pool)
{
	struct link_t* link;
	if (!pool->first)
		return NULL;
	link = pool->first;
	pool->first = link->next;
	
	return (void*)((uintptr_t)link - pool->link_offset);
}


void queue_init(struct queue_t* q, int link_offset)
{
	memset(q, 0, sizeof *q);
	q->link_offset = link_offset;
}

void queue_enqueue(struct queue_t* q, void* obj)
{
	struct link_t* link;

	link = (struct link_t*)((uintptr_t)obj + q->link_offset);
	
	link->next = NULL;
	q->last = link;
	if (!q->first)
		q->first = link;
}

void* queue_dequeue(struct queue_t* q)
{
	struct link_t* link;
	if (!q->first)
		return NULL;
	link = q->first;
	q->first = link->next;
	
	return (void*)((uintptr_t)link - q->link_offset);
}


